//
//  SetUpTopView.m
//  markmap
//
//  Created by 甘世清 on 2024/4/18.
//

#import "SetUpTopView.h"

@implementation SetUpTopView

- (void)SQ_UIViewForSelfView{
    [self addSubview:self.avatarImage];
    [self.avatarImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(88);
        make.top.equalTo(self).offset(20);
        make.centerX.equalTo(self);
    }];
    
    UILabel * la = [[UILabel alloc]init];
    la.text = SQNSLocalString(@"点击修改头像");
    la.font = FONT_MEDIUM(14);
    la.textColor = UIColor.whiteColor;
    [self addSubview:la];
    [la mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self.avatarImage.mas_bottom).offset(10);
    }];
    
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.backgroundColor = UIColor.clearColor;
    [btn addTarget:self action:@selector(SQ_PushPeople) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    UserModel * model = [UserModel share];
    if ([model.portrait isNotEmpty]) {
        [self.avatarImage sd_setImageWithURL:SQURLImage(model.portrait) placeholderImage:SQGetImage(@"zanwutouxiang")];
    } else {
        self.avatarImage.image = SQGetImage(@"zanwutouxiang");
    }
}
-(void)SQ_PushPeople{
    
    WS(weakSelf);
  
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:SQNSLocalString(@"设置头像") message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction * co0 = [UIAlertAction actionWithTitle:SQNSLocalString(@"拍照") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf SQ_leftButtonClick];
    }];
    UIAlertAction * co1 = [UIAlertAction actionWithTitle:SQNSLocalString(@"相册") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf SQ_rightButtonClick];
    }];
    UIAlertAction * co2 = [UIAlertAction actionWithTitle:SQNSLocalString(@"取消") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:co0];
    [alert addAction:co1];
    [alert addAction:co2];
    [[Manager SQ_getCurrentViewController] presentViewController:alert animated:YES completion:nil];
}

-(void)SQ_leftButtonClick{
    [[Manager SQ_getCurrentViewController] presentViewController:self.imagePickerVc animated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissViewControllerAnimated:YES completion:nil];
    WS(weakSelf);
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    if ([type isEqualToString:@"public.image"]) {
        UIImage *photo = [info objectForKey:UIImagePickerControllerEditedImage];
        if (photo == nil)
            {
                photo = [info objectForKey:UIImagePickerControllerOriginalImage];
            }
        [weakSelf SQ_caijian:photo];
    }else
    {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"相册数据异常")];
    }
}

-(void)SQ_rightButtonClick{
    WS(weakSelf);
    TZImagePickerController * vc = [[TZImagePickerController alloc]init];
    vc.maxImagesCount = 1;
    [[Manager SQ_getCurrentViewController] presentViewController:vc animated:YES completion:nil];
    [vc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        [weakSelf SQ_caijian:photos.firstObject];
    }];
}
//上传相册
-(void)SQ_caijian:(UIImage*)image{
    WS(weakSelf);
    //上传头像
    [YJProgressHUD SQ_showMsgWithoutView:@""];
    [HttpTool SQ_Image:image successBlock:^(BaseModel * _Nonnull httpModel) {
        [weakSelf SQ_setUpName:httpModel.data];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}
-(void)SQ_setUpName:(NSString*)header{
    [YJProgressHUD SQ_showMsgWithoutView:@""];
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    if (![header isNotEmpty]) {
        return;
     }
    [dic setObject:header forKey:@"portrait"];
    [HttpTool post:Url5 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"头像已更新,审核后方可生效")];
        [self.avatarImage sd_setImageWithURL:SQURLImage(header) placeholderImage:SQGetImage(@"zanwutouxiang")];
        UserModel * model = [UserModel share];
        model.portrait = header;
        [UserModel updateLocalUserModel:model];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}

- (UIImagePickerController *)imagePickerVc{
    if (!_imagePickerVc) {
        _imagePickerVc = [[UIImagePickerController alloc] init];
        _imagePickerVc.delegate = self;
        _imagePickerVc.allowsEditing = YES;
        UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
        if ([UIImagePickerController isSourceTypeAvailable: sourceType]) {
            _imagePickerVc.sourceType = sourceType;
        }
    }
    return _imagePickerVc;
}

- (UIImageView *)avatarImage{
    if (!_avatarImage) {
        _avatarImage = [[UIImageView alloc]init];
        _avatarImage.contentMode = UIViewContentModeScaleAspectFill;
        _avatarImage.clipsToBounds = YES;
        _avatarImage.layer.cornerRadius = 44;
        _avatarImage.layer.borderColor = UIColor.whiteColor.CGColor;
        _avatarImage.layer.borderWidth = 1;
        _avatarImage.image = SQGetImage(@"zanwutouxiang");
    }
    return _avatarImage;
}
@end
